package fr.demo.list;

import java.util.ArrayList;
import java.util.List;

import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ContextMenu.ContextMenuInfo;
import android.widget.AdapterView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;

public class DetailCustomList extends ListActivity implements OnItemClickListener {
	/**
	 * Adapter of the ListView
	 *  - Controler of the list, you must use it for ADD/DELETE/MODIFY element
	 */
	private ContactAdapter adapter; 
	/**
	 * Current position selected in the list
	 */
	private int current = -1;
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        /* Populate list */
        List<Contact> data = new ArrayList<Contact>();
        for(int i = 0;i<20;i++){
        	Contact c = new Contact("Name "+i,"FirstName "+i);
        	data.add(c);
        }
        
        /* Instanciate adapter with XML Layout representing on Item */
        adapter = new ContactAdapter(
        		this,
        		R.layout.item_cell_2,
        		data);
        getListView().setAdapter(adapter);
        
        /* Bind the Click event */
        getListView().setOnItemClickListener(this);
        /* Bind the ContextMenu event */
        getListView().setOnCreateContextMenuListener(this);
    }

	public void onItemClick(AdapterView<?> ad, View cell_view, int position, long id) {
		// TODO Auto-generated method stub
		Intent intent = new Intent(DetailCustomList.this,Information.class);
		intent.putExtra("OTHER", adapter.getItem(position));
		startActivity(intent);
	}
	
	/**
	 * OptionMenu, ContextMenu and TOAST
	 */
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// TODO Auto-generated method stub
		getMenuInflater().inflate(R.menu.options, menu);
		return super.onCreateOptionsMenu(menu);
	}

	/**
	 * Create the context Menu
	 */
	@Override
	public void onCreateContextMenu(ContextMenu menu, View v,
			ContextMenuInfo menuInfo) {
		// TODO Auto-generated method stub
		/**
		 *  Creation of the Context Menu for the list view
		 *  You can get the item position where user has made
		 *  a long click (the cell position) with object
		 *  menuInfo (see example below)
		 *   
		 */
		/* Create the Context Menu */
		getMenuInflater().inflate(R.menu.context, menu);
		/* Set the title of the ContextMenu with the name of cell */
		AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo)menuInfo;
		Contact c = (Contact)adapter.getItem(info.position);
		menu.setHeaderTitle(c.getName() + " - " + c.getFirstName());
		current = info.position;
		
		super.onCreateContextMenu(menu, v, menuInfo);
	}

	/**
	 * Click event from the context menu
	 */
	@Override
	public boolean onContextItemSelected(MenuItem item) {
		// TODO Auto-generated method stub
		switch(item.getItemId()){
		case R.id.delete:
			Toast.makeText(this, "Item deleted", Toast.LENGTH_SHORT);
			adapter.remove(adapter.getItem(current));
			break;
		}
		return super.onContextItemSelected(item);
	}




	/**
	 * Click event on the OptionsMenu
	 */
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// TODO Auto-generated method stub
		switch(item.getItemId()){
		case R.id.add :
			Toast.makeText(this, "Item Added", Toast.LENGTH_SHORT);
			Contact c = new Contact("NEW ENTRY N" + adapter.getCount(), "NEW ENTRY F" + adapter.getCount());
			adapter.add(c);
			break;
		}
		return super.onOptionsItemSelected(item);
	}
	
}